#if __has_include(<SEGMasonry/SEGMasonry.h>)
#import <SEGMasonry/SEGMasonry.h>
#else
#import "SEGMasonry.h"
#endif

#if __has_include(<SEGTool/UIColor+SEGCategory.h>)
#import <SEGTool/UIColor+SEGCategory.h>
#else
#import "UIColor+SEGCategory.h"
#endif

#import "SEGContentTableViewCell.h"

@interface SEGContentTableViewCell ()
@end

@implementation SEGContentTableViewCell
#pragma mark - Life Cycle
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self segInitViews];
    }
    return self;
}

-(void)dealloc {
    NSLog(@"%s dealloc",__FUNCTION__);
}

#pragma mark - Layout
- (void)updateConstraints {
    [super updateConstraints];
}

#pragma mark - Public Methods

+(instancetype)cellWithTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath{
    SEGContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier]];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self reuseIdentifier]];
    }
    cell.indexPath = indexPath;
    return cell;
}

#pragma mark - Private Methods

+(NSString*)reuseIdentifier{
    return NSStringFromClass([self class]);
}

-(void)segInitViews{
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    // self.contentView addSubview
    [self.contentView addSubview:self.titleLabel];

    // masonry...
    [self.titleLabel seg_makeConstraints:^(SEGConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
    }];

}

// tell UIKit that you are using AutoLayout
+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    //@weakify(self);

    // {@"code":@(0),@"msg":@""}
    [self.viewModel.messageSubject subscribeNext:^(NSDictionary*  _Nullable x) {
        //@strongify(self);
        NSInteger code = [[x valueForKey:@"code"] integerValue];
        NSString *msg = [x valueForKey:@"msg"];

        switch(code) {
            case 0: 
            NSLog(@"%@",msg);
            break;
            default:
                break;
        }
    }];

    [[[self.viewModel.requestCommand executionSignals] switchToLatest] subscribeNext:^(NSNumber*  _Nullable x) {
        //@strongify(self);
    }];

    [self.viewModel.requestCommand.errors subscribeNext:^(NSError * _Nullable x) {
        //@strongify(self);
    }];

    // [self.viewModel.requestCommand execute:nil];

    RAC(self.titleLabel,text) = RACObserve(self.viewModel, titleString);
    // UITableViewCell中使用下面
    // RAC(self.titleLabel,text) = [RACObserve(self.viewModel, titleString) takeUntil:self.rac_prepareForReuseSignal]; 

}

#pragma mark - getter
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UIColor.seg_color1;
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.text = @"";
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.numberOfLines = 1;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    return _titleLabel;
};

#pragma mark setter
-(void)setViewModel:(SEGContentTableViewCellViewModel *)viewModel{
    _viewModel = viewModel;
    if (!viewModel)  return;
    [self bindViewModel];
}
@end
